package com.pic.others.controller;


import com.pic.common.utils.R;
import com.pic.others.domain.dto.history.HistoryUserDTO;
import com.pic.others.domain.dto.picture.PictureInfoDTO;
import com.pic.others.domain.dto.picture.PictureWithCategoryDTO;
import com.pic.others.domain.dto.user.UserCapacityDTO;
import com.pic.others.domain.po.Picture;
import com.pic.others.domain.po.RecommendSimilarPicture;
import com.pic.others.domain.vo.picture.PictureDeleVO;
import com.pic.others.service.IPictureService;
import com.pic.others.service.IRecommendSimilarPictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/picture")
@Api("关于图片操作")
public class PictureController {

    private final IPictureService pictureService;
    private final IRecommendSimilarPictureService recommendSimilarPictureService;


    //返回路径，时间，图片id,时间
    @SneakyThrows
    @ApiOperation("上传单张图片")
    @PostMapping("/fileupload")
    public R<Picture> FileUpload(HttpServletRequest request, MultipartFile file) {
        return pictureService.fileupload(file);
    }


    @SneakyThrows

    @ApiOperation("上传多张图片")
    @PostMapping("/fileuploads")
    public R<String>fileUploads(@RequestParam("files")List<MultipartFile>files){
        return pictureService.fileuploads(files);
    }
//    public R<String>fileUploads(@RequestParam("files")MultipartFile[]files){
//        return pictureService.fileuploads(files);
//    }


    //有bug，要将图片进行移动到回收站，没有做
    //注意路径是userId+picture
    @ApiOperation("删除一张图片")
    @PostMapping("/deletePictureByLocate")
    public R<String>deletePictureByLocate(@RequestBody PictureDeleVO vo){
        log.info("vo:{}",vo);
        log.info("删除一张图片");
//        return null;
        return pictureService.deletePictureByLocate(vo);
    }



    @ApiOperation("删除多张图片")
    @PostMapping("/deletePictureByLocates")
    public R<String>deletePictureByLocates(@RequestBody List<PictureDeleVO> voList){
        log.info("你进入了多删图片");
        return pictureService.deletePictureByLocates(voList);
    }

    @ApiOperation("获取多张图片以时间方式分组显示")
    @PostMapping("/getManyPicturesByLocalDate")
    public R<Map<LocalDate,List<Picture>>>getMantPictures(){
        return pictureService.getManyPictures();
    }

    @ApiOperation("获取多张图片以分类分组的形式展示")
    @GetMapping("/getManyPictureByCategory")
    public R<PictureWithCategoryDTO>getManyPictureByCategory(@RequestParam(value = "categoryId",required = false,defaultValue ="-1" )
                                                        long categoryId){
        //如果category没有传参，则默认显示父节点是自己的节点，
        //如果有传参，就进行判断处理
        //如果存在子节点，就遍历到,子节点为空之后的节点
        //如果不存在，则查找picture中categoryId为这个的图片，
        return pictureService.getManyPictureByCategory(categoryId);
    }


//    @ApiOperation("识别图片文字")
//    @GetMapping("/{locate}")
//    public R<String> getTextByTess4J(@PathVariable("locate")String locate){
//        return pictureService.getTextByTess4J(locate);
//    }

    @ApiOperation("给出相似图片删除推荐")
    @PostMapping("/deleteByAdvise")
    public R<String>deleteByAdvise(@RequestBody List<Long>ids){
        return recommendSimilarPictureService.deleteByAdvise(ids);
    }

    @ApiOperation("给出相似图片推荐")
    @GetMapping("/getSimilarPictureByUserId")
    public R<Map<LocalDate,List<RecommendSimilarPicture>>>getManySimilar(){
        return recommendSimilarPictureService.getManySimilar();
    }
    @ApiOperation("初始即调用")
    @GetMapping("/initRecommand")
    public R<Map<Long,List<RecommendSimilarPicture>>>initRecommand(){
        return recommendSimilarPictureService.initRecommand();
    }

//    @ApiOperation("给出picture的详情")
//    @GetMapping("/getSimilarInfoPicture/{recommannd}")
//    public R<SimilarInfoDTO>getSimilarBySimilarId(@PathVariable() Long recommannd){
//        return null;
//    }





    @ApiOperation("自动美化图片")
    public R<String>getAutoBeautifyPicture(Picture picture){
        return pictureService.getAutoBeautifyPicture(picture);
    }
//
//    @ApiOperation("跨模态或者是文字搜索")
//    @GetMapping("/getPictureByFeatures")
//    public Set<Picture> getPictureByFeatures(String information){
//        log.info(information);
//        return null;
//    }

    @ApiOperation("拍摄时间查找图片")
    @GetMapping("/getPicturesByLocalDate")
    public R<List<Picture>>getPicturesByLocalDate(LocalDate localDate){
        return pictureService.getPicturesByLocalDate(localDate);
    }


    @ApiOperation("下载单张图片")
    @PostMapping("downloadpicture")
    public R<String>downLoadPicture(String locate){
        return pictureService.downLoadPicture(locate);
    }

    @ApiOperation("下载多张图片")
    @PostMapping("downloadpictures")
    public R<String>downLoadPictures(List<String> locates){
        return pictureService.downLoadPictures(locates);
    }



    @ApiOperation("我也不懂他的要求，只知道lsl要这么做")
    @PostMapping("/getPictureByPictureId")
    public R<PictureInfoDTO> getPictureByPictureId(@RequestBody long  pictureId) {
        log.info("locate: {}", pictureId);
        return pictureService.getPictureByPictureId(pictureId);
    }

    @ApiOperation("获取用户的历史记录")
    @GetMapping("/gethistories")
    public R<HistoryUserDTO>getUserHistories(){
        log.info("获取用户历史记录");
        return pictureService.getUserHistories();
    }

    @ApiOperation("删除用户的单个历史记录")
    @DeleteMapping("/deletehistory")
    public R<String>DeleteHistory(String info){
        return pictureService.deleteHistory(info);
    }


    @ApiOperation("删除用户的全部历史记录")
    @PostMapping("/deletehistories")
    public R<String>DeleteHistories(){
        return pictureService.deleteHistories();
    }

    @ApiOperation("对图片进行搜索")
    @PostMapping("/getPictureByWord")
    public R<List<Picture>>getPictureByWord( @RequestBody  String information){
        return pictureService.getPictureByWord(information);
    }

    @ApiOperation("对图片进行磨皮")
    @PostMapping("/getPictureBySmoothing")
    public R<Picture>getPictureBySmoothing(){
        return pictureService.getPictureBySmoothing();
    }

    @ApiOperation("对图片进行调整清晰度需要有具体实例，使用实例对照应用场景")
    @PostMapping("/getPictureByClarity")
    public R<Picture>getPictureByClarity(){
        return pictureService.getPictureByClarity();
    }

    @ApiOperation("对图片进行风格修改")
    @PostMapping("/getPictureByOtherStyle")
    public R<Picture>getPictureByOtherStyle(){
        return pictureService.getPictureByOtherStyle();
    }

    @ApiOperation("图片上传")
    @PostMapping("/uploadPictures")
    public R<List<Picture>> uploadPicture() throws InterruptedException {
        List<Picture> pictures = new ArrayList<>();

        // 生成1到3之间的随机休眠时间（单位：毫秒）
        int sleepTime = (int) (1000 + Math.random() * 2000);

        Thread.sleep(sleepTime);

        return R.success(pictures);
    }
    @ApiOperation("获取用户的容量和大小")
    @GetMapping("/getUserCapacity")
    public R<UserCapacityDTO>getUserCapacity(){
        return pictureService.getUserCapacity();
    }



}
